package com.gcit.seniorWish.web.modular.examinee.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.core.node.SelectNode;
import com.gcit.seniorWish.web.common.constant.Const;
import com.gcit.seniorWish.web.common.constant.cache.Cache;
import com.gcit.seniorWish.web.common.constant.cache.CacheKey;
import com.gcit.seniorWish.web.common.constant.enums.ManagerStatusEnum;
import com.gcit.seniorWish.web.common.constant.enums.RoleEnum;
import com.gcit.seniorWish.web.common.constant.enums.UserTypeEnum;
import com.gcit.seniorWish.web.common.constant.factory.ConstantFactory;
import com.gcit.seniorWish.web.common.persistence.dao.SeniorSchoolMapper;
import com.gcit.seniorWish.web.common.persistence.model.Role;
import com.gcit.seniorWish.web.common.persistence.model.SeniorSchool;
import com.gcit.seniorWish.web.common.persistence.model.User;
import com.gcit.seniorWish.web.core.shiro.ShiroKit;
import com.gcit.seniorWish.web.modular.examinee.dao.SeniorSchoolDao;
import com.gcit.seniorWish.web.modular.examinee.service.ISeniorSchoolService;

import cn.hutool.core.util.StrUtil;

import com.gcit.seniorWish.web.modular.system.service.IRoleService;
import com.gcit.seniorWish.web.modular.system.service.IUserMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

@Service
public class SeniorSchoolServiceImpl extends ServiceImpl<SeniorSchoolMapper, SeniorSchool>
		implements ISeniorSchoolService {

	@Resource
	private SeniorSchoolDao seniorSchoolDao;

	@Resource
	private SeniorSchoolMapper seniorSchoolMapper;

	@Autowired
	private IUserMgrService userMgrService;

	@Autowired
	private IRoleService roleService;

	/**
	 * 查询学校列表
	 *
	 * @param schoolId
	 *            学校id
	 * @author tonglu
	 * @date 2018-03-19 下午 3:22
	 */
	@Override
	public List<SelectNode> listSchoolForSelect2(String term,String schoolId) {
		List<SelectNode> selectList = new ArrayList<>();
		List<SeniorSchool> schools = null;
		if (StrUtil.isNotBlank(schoolId)) {
			if(StrUtil.isNotBlank(term)){
				term="%"+term+"%";
			}
			schools = seniorSchoolDao.listSchool(term,schoolId);
			SelectNode node = new SelectNode();
			node.setId(schools.get(0).getId());
			node.setText(schools.get(0).getSchoolCode() +":"+schools.get(0).getSchoolName());
			selectList.add(node);
		} else {
			EntityWrapper<SeniorSchool> ew = new EntityWrapper<SeniorSchool>();
			if (StrUtil.isNotBlank(term)) {
				ew.andNew().like("school_name", term).or().like("school_code", term);
			}
			schools = seniorSchoolMapper.selectList(ew);
			for (SeniorSchool s : schools) {
				SelectNode node = new SelectNode();
				node.setId(s.getId());
				node.setText(s.getSchoolCode() +":"+s.getSchoolName());
				selectList.add(node);
			}
		}
		return selectList;
	}

	/**
	* 合并初中学信息
	* @param seniorSchool 初中信息
	* @author gaozhichang
	* @date 2018-03-29 下午 1:40
	*/
	@Override
	@Transactional
	@CacheEvict(value = Cache.BUSS, key = "'" + CacheKey.SENIOR_SCHOOL + "'+#seniorSchool.id")
	public void mergeSeniorSchool(SeniorSchool seniorSchool){
		if (StrUtil.isBlank(seniorSchool.getId())) {
			this.seniorSchoolMapper.insert(seniorSchool);
			// 插入用户表信息
			User user = new User();
			user.setType(UserTypeEnum.SCHOOL.getValue());
			user.setAccount("szwz" + seniorSchool.getSchoolCode());
			user.setName(seniorSchool.getSchoolName());
			// 完善账号信息
			user.setSalt(ShiroKit.getRandomSalt(5));
			user.setPassword(ShiroKit.md5(Const.SCHOOLORCLASS_PWD, user.getSalt()));
			user.setDeptId(Const.DEFAULT_DEPT_ID);
			user.setStatus(ManagerStatusEnum.ABLE.getCode());
			user.setSchoolId(seniorSchool.getId());
			userMgrService.insert(user);
			Role role = roleService.selectOne(Condition.create().eq("name", RoleEnum.SCHOOL_ADMIN.getDes()));
			userMgrService.setRoles(user.getId(), role.getId());
		} else {
			this.seniorSchoolMapper.updateById(seniorSchool);
			//根据登录名查询对应的用户，更改班级名称
			SeniorSchool existSchool = ConstantFactory.me().getSeniorSchoolById(seniorSchool.getId());
			User user = userMgrService.selectOne(Condition.create().setSqlSelect("id,name").eq("account",
					"szwz" + existSchool.getSchoolCode()));
			//当用户存在并且名称不同了。修改用户信息
			if (user != null) {
				user.setName(seniorSchool.getSchoolName());
				userMgrService.updateById(user);
			}
		}
	}
}
